-
Notifications
You must be signed in to change notification settings - Fork 14
Add notimestamp linter #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Karthik-K-N The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike the nophase
linter, this linter has a suggested replacement for the substring timestamp
(time
).
At the very least, we should ensure that we include that replacement suggestion in the report message.
pkg/analysis/notimestamp/analyzer.go
Outdated
// First check if the struct field name contains 'timestamp' | ||
if strings.Contains(strings.ToLower(fieldName), "timestamp") { | ||
pass.Reportf(field.Pos(), | ||
"field %s: fields with timestamp substring should be avoided", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably include in the message what alternative should be used in place of this. I believe the appropriate replacement here would be time
.
Seeing as how this linter has a proposed replacement, should we also implement the suggested fix for this so that if someone were to run lint --fix
that it would automatically apply the suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey, A quick question, whats the right way of debugging or to get to know the value of some variable, If I try
fmt.Println(fieldName)
I dont see anything getting printed on the console
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are you trying to debug it? Running the actual tool or via the tests?
If it is through the tests, you'd want to tell it to be verbose using the -v
flag with go test
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ok, I will give a try, Thanks, I was trying with tool
./bin/golangci-kube-api-linter run kube-api-linter/pkg/analysis/notimestamp/testdata/src/a/a.go --fix
9957ad0
to
f751830
Compare
Updated the PR to address review comments, Couple of pending questions
twice to fix the names, Once for fixing field names and second time is for fixing json tags. Not sure thats ideal.
|
f751830
to
e462791
Compare
docs/linters.md
Outdated
|
||
The `notimestamp` linter checks that the fields in the API types don't contain a 'Timestamp', or any field which contains 'Timestamp' as a substring, e.g CreateTimestamp. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe?
The `notimestamp` linter checks that the fields in the API types don't contain a 'Timestamp', or any field which contains 'Timestamp' as a substring, e.g CreateTimestamp. | |
The `notimestamp` linter checks that the fields in the API are not named with the word 'Timestamp'. |
What is the reason for this from the API conventions, might be good to help explain that context here a little?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any other place apart from https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#naming-conventions, To look for reasons for avoiding it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would generally try to use git blame to work out the PR that introduced the rule, and see if there is additional context there. If not, perhaps #api-reviews on the Kuberentes slack might be able to help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure thanks, I will check
return | ||
} | ||
|
||
var suggestedFixes []analysis.SuggestedFix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes should be explained in the linters documentation, see how others have done that
e462791
to
627e267
Compare
|
||
The `notimestamp` linter checks that the fields in the API are not named with the word 'Timestamp'. | ||
|
||
The name of a field that specifies the time at which something occurs should be called `somethingTime`.Its recommended not use stamp (e.g., creationTimestamp). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of a field that specifies the time at which something occurs should be called `somethingTime`.Its recommended not use stamp (e.g., creationTimestamp). | |
The name of a field that specifies the time at which something occurs should be called `somethingTime`. It is recommended not use stamp (e.g., creationTimestamp). |
// Initializer returns the AnalyzerInitializer for this | ||
// Analyzer so that it can be added to the registry. | ||
func Initializer() initializer { | ||
return initializer{} | ||
} | ||
|
||
// intializer implements the AnalyzerInitializer interface. | ||
type initializer struct{} | ||
|
||
// Name returns the name of the Analyzer. | ||
func (initializer) Name() string { | ||
return name | ||
} | ||
|
||
// Init returns the intialized Analyzer. | ||
func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) { | ||
return Analyzer, nil | ||
} | ||
|
||
// Default determines whether this Analyzer is on by default, or not. | ||
func (initializer) Default() bool { | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A recent refactor has changed the way functions init, can you please rebase and look at another, non-configurable linter to see how this should look now.
commentstart
is probably a good example to crib from
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure will do that
@@ -86,6 +87,7 @@ func NewRegistry() Registry { | |||
nofloats.Initializer(), | |||
nomaps.Initializer(), | |||
nophase.Initializer(), | |||
notimestamp.Initializer(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will conflict when you rebase, we don't maintain this list anymore as each linter now self registers by blank importing in a package called registration
fieldReplacementName := timeStampRegEx.ReplaceAllString(fieldName, "Time") | ||
if fieldReplacementName != fieldName { | ||
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{ | ||
Message: "replace field with Time", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these normally have to be unique, have you tried this against a file that has multiple failures using a full golangci-lint run?
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Adds a new
notimestamp
linter that checks for any fields in struct having timestamp string or substring.Fixes: #26